/*
Copyright (c)2009 Seesmic, Inc

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package com.seesmic.twhix
{
	public class JID
	{
		public var user:String = '';
		public var host:String = '';
		public var resource:String = '';
		
		public function JID(jid:String=null)
		{
			if(jid) fromString(jid);
		}
	
		public function fromString(jid:String):void {
			if(!jid) return;
			var tmp:Array;
			if(jid.search('@') > 0) {
				tmp = jid.split('@');
				user = tmp[0];
				host = tmp[1];
			} else {
				host = jid;
			}
			if(host.search('/') > 0) {
				tmp = host.split('/');
				resource = tmp[1];
				if(!resource) resource = ''
				host = tmp[0];
			}
		}
		
		public function getResource():String {
			return resource;
		}
		
		public function isSet():Boolean {
			if(this.user != '' || this.host != '') {
				return true;
			}
			return false;
		}
		
		public function getBareJID():String {
			var out:String;
			if(user) {
				out = user + '@' + host;
			} else {
				out = host; 
			}
			return out;
		}
		
		public function toString():String {
			var out:String = getBareJID();
			if(resource) out += '/' + resource;
			return out;
		}
	}
}
